using System;
using System.Windows.Forms;
using WegasManagementSystem.BLL;
using WegasManagementSystem.Models;

namespace WegasManagementSystem.Forms
{
    public partial class LoginForm : Form
    {
        private UserBLL userBLL;

        public LoginForm()
        {
            InitializeComponent();
            userBLL = new UserBLL();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            // 设置窗口标题
            this.Text = "高校工资管理系统 - 登录";
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            // 验证输入
            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                MessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUsername.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPassword.Focus();
                return;
            }

            try
            {
                // 验证用户登录
                User user = userBLL.ValidateUser(txtUsername.Text, txtPassword.Text);

                if (user != null)
                {
                    // 保存登录用户信息到全局配置
                    GlobalConfig.CurrentUser = user;

                    // 登录成功
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误，请重试。", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPassword.Clear();
                    txtPassword.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("登录时发生错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
} 